package com.attendance.controller;

import com.attendance.java.DateUtils;
import com.attendance.pojo.UserDetails;
import com.attendance.result.LayuiMiniPage;
import com.attendance.security.user.pojo.UserInfo;
import com.attendance.service.UserDetailsServiceImpl;
import com.attendance.web.Result;
import com.attendance.web.ResultCode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户控制器
 *
 * @author 鵺丶
 * @date 2020/12/30
 */
@Controller
@RequestMapping("/system/user")
@RolesAllowed("ADMIN")
public class UserController {

    @Autowired
    private UserDetailsServiceImpl userDetailsService;

    @GetMapping()
    public String table() {
        return "/system/user/table";
    }

    @GetMapping("/add")
    public String add() {
        return "/system/user/add";
    }

    @GetMapping("/edit/{uid}")
    public ModelAndView editForId(@PathVariable Long uid) {
        ModelAndView modelAndView = new ModelAndView("/system/user/edit");
        UserDetails userDetails = userDetailsService.selectById(uid);
        modelAndView.addObject("user", userDetails);
        modelAndView.addObject("dateBirth", DateUtils.date(userDetails.getDateBirth()));
        return modelAndView;
    }

    @GetMapping("/edit")
    public String edit() {
        return "/system/user/edit";
    }

    @GetMapping("/editPass")
    public String editPass() {
        return "/system/user/editPass";
    }

    @ResponseBody
    @GetMapping("/list")
    public List<UserDetails> getList() {
        return userDetailsService.getUserDetailsList();
    }

    @ResponseBody
    @GetMapping("/listPage")
    public LayuiMiniPage<UserDetails> getListPage(Integer page, Integer limit, String searchParams) {
        if (searchParams != null) {
            Page<UserDetails> listPage = userDetailsService.getListPage(page, limit, searchParams);
            return new LayuiMiniPage<>(200, "查询成功", listPage.getTotal(), listPage.getRecords());
        }
        Page<UserDetails> listPage = userDetailsService.getListPage(page, limit);
        return new LayuiMiniPage<>(200, "查询成功", listPage.getTotal(), listPage.getRecords());
    }

    @ResponseBody
    @GetMapping("/listPage/{dId}")
    public LayuiMiniPage<UserDetails> getListPage(Integer page, Integer limit, String searchParams, @PathVariable Long dId) {
        try {
            if (searchParams != null) {
                Page<UserDetails> listPage = userDetailsService.getListPage(page, limit, searchParams, dId);
                return new LayuiMiniPage<>(200, "查询成功", listPage.getTotal(), listPage.getRecords());
            }
            Page<UserDetails> listPage = userDetailsService.getListPage(page, limit, dId);
            return new LayuiMiniPage<>(200, "查询成功", listPage.getTotal(), listPage.getRecords());
        } catch (Exception e) {
            return new LayuiMiniPage<>(500, e.getMessage(), 0L, null);
        }
    }

    @ResponseBody
    @PostMapping("/update")
    public Result update(UserDetails user) {
        if (userDetailsService.updateUser(user)) {
            return new Result(ResultCode.SUCCESS);
        } else {
            return new Result(ResultCode.FAIL);
        }
    }

    @ResponseBody
    @PostMapping("/updatePass")
    public Result updatePass(String oldPassword, String newPassword) throws Exception {
        try {
            if (userDetailsService.updatePass(oldPassword, newPassword)) {
                return new Result(ResultCode.SUCCESS);
            } else {
                return new Result(ResultCode.FAIL);
            }
        } catch (Exception e) {
            return new Result(502, e.getMessage(), false);
        }
    }

    @ResponseBody
    @PutMapping("/add")
    public Result add(UserDetails user, UserInfo userInfo) {
        try {
            if (userDetailsService.addUser(user, userInfo)) {
                return new Result(ResultCode.SUCCESS);
            } else {
                return new Result(ResultCode.FAIL);
            }
        } catch (Exception e) {
            return new Result(502, e.getMessage(), false);
        }
    }

    @DeleteMapping("delete")
    @ResponseBody
    public Result delete(@RequestParam("ids[]") ArrayList<Integer> ids) {
        if (userDetailsService.deleteUsers(ids)) {
            return new Result(ResultCode.SUCCESS);
        } else {
            return new Result(ResultCode.FAIL);
        }
    }

    @DeleteMapping("delete/{id}")
    @ResponseBody
    public Result delete(@PathVariable("id") Long id) {
        if (userDetailsService.deleteUser(id)) {
            return new Result(ResultCode.SUCCESS);
        } else {
            return new Result(ResultCode.FAIL);
        }
    }
}
